package com.tyk.bookstore.back.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.model.type.SuggestionQuery;
import com.tyk.bookstore.back.common.core.mvc.annotation.UnderlineToCamelModel;
import com.tyk.bookstore.back.system.model.query.UserAddQuery;
import com.tyk.bookstore.back.system.model.query.UserPageQuery;
import com.tyk.bookstore.back.system.model.query.UserUpdateQuery;
import com.tyk.bookstore.back.system.model.vo.UserPageVo;
import com.tyk.bookstore.back.system.model.vo.UserVo;
import com.tyk.bookstore.back.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "系统管理/用户管理", description = "用户管理")
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("api/system/user")
public class UserController {

    private final UserService userService;

    @Operation(summary = "分页查询获取用户列表", description = "分页查询获取用户列表")
    @SaCheckPermission("system:user:page")
    @GetMapping("page")
    public R<UserPageVo> getPage(
            @Validated @UnderlineToCamelModel @ParameterObject UserPageQuery query
    ) {
        return R.ok(userService.getPage(query));
    }

    @Operation(summary = "根据ID获取用户信息", description = "根据ID获取用户信息")
    @SaCheckPermission("system:user:get")
    @GetMapping("{id}")
    public R<UserVo> getById(
            @Validated @Min(0) @PathVariable("id") Long id
    ) {
        return R.ok(userService.getById(id));
    }

    @Operation(summary = "新增用户", description = "新增用户")
    @SaCheckPermission("system:user:add")
    @PostMapping("")
    public R<Object> add(
            @Validated @RequestBody UserAddQuery query
    ) {
        userService.addUser(query);
        return R.ok();
    }

    @Operation(summary = "修改用户信息", description = "修改用户信息")
    @SaCheckPermission("system:user:update")
    @PutMapping("")
    public R<Object> updateById(
            @Validated @RequestBody UserUpdateQuery query
    ) {
        userService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "根据ID删除用户信息", description = "根据ID删除用户信息")
    @SaCheckPermission("system:user:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Validated @NotNull @Min(0) @PathVariable("id") Long id
    ) {
        userService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "查询用户搜索建议列表", description = "查询用户搜索建议列表")
    @SaCheckPermission({"system:user:suggestion:list"})
    @GetMapping({"suggestion/list"})
    public R<List<BinVo>> getSuggestionList(
            @Validated @ParameterObject SuggestionQuery query
    ) {
        return R.ok(userService.getSuggestionList(query));
    }
}
